
#ifndef __CRC16_H__
#define __CRC16_H__

#include <stdint.h> 

static uint8_t crc_16(const uint8_t *data, uint16_t len)
{
    uint16_t crc = 0xFFFF;
    for(uint8_t i = 0; i < len; i++) {
        crc ^= data[i];
        for(uint8_t j = 0; j < 8; j++) {
            uint16_t carry_flag = crc & 0x0001;
            crc >>= 1;
            if (carry_flag) {
                crc ^= 0xA001;
            }
        }
    }
    return crc;
}

static uint8_t checksum(const uint8_t *data, uint8_t len)
{
    uint8_t sum = 0;
    len--;
    
    for (int index = 0; len-index >= 0; index++) {
        sum += data[len-index];
    }
    sum = ~sum;
    sum += 1;
    return sum;
}

#endif
